package com.bunkerdev.fingermania.utils;

import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class GIFView extends View {
	private Movie movie;
	private long moviestart;
	private int x = 0;
	private int y = 0;

	public GIFView(Context context) {
		super(context);
		try {
			movie = Movie.decodeStream(getResources().getAssets().open(
					"arrowanimation.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public GIFView(Context context, AttributeSet attrs) {
		super(context, attrs);
		try {
			movie = Movie.decodeStream(getResources().getAssets().open(
					"arrowanimation.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public GIFView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		try {
			movie = Movie.decodeStream(getResources().getAssets().open(
					"arrowanimation.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
	
	public void draw(Canvas canvas){	
		long now = android.os.SystemClock.uptimeMillis();
		Paint p = new Paint();
		p.setAntiAlias(true);
		if (moviestart == 0)
			moviestart = now;
		int relTime;
		relTime = (int) ((now - moviestart) % movie.duration());
		movie.setTime(relTime);
		movie.draw(canvas, x, y);
		this.invalidate();
	}
}
